KERNEL_VERSION_BY_BUILDENV=$(shell uname -r)
KERNEL_PATH := /lib/modules/$(KERNEL_VERSION_BY_BUILDENV)/build

obj-m += mysm3/
obj-m += mytest/

DIRS := $(shell find . -maxdepth 3 -type d)
TARGET = $(foreach dir,$(DIRS),$(wildcard \
	$(dir)/*.o) $(dir)/*.ko $(dir)/*.tmp_versions $(dir)/*.depend $(dir)/*.mod.c $(dir)/*.order $(dir)/*.symvers)

default: 
	$(MAKE) -C $(KERNEL_PATH) M=$(shell pwd) modules \
		CONFIG_MYSM3=m \
		CONFIG_MYTEST=m

install:
	$(shell mkdir -p /lib/modules/$(KERNEL_VERSION_BY_BUILDENV)/extra)
	$(shell find . -name "*.ko" -exec cp -f {} /lib/modules/$(KERNEL_VERSION_BY_BUILDENV)/extra \;)
	depmod -a
	$(shell if test -e /etc/modprobe.d/10-unsupported-modules.conf; then \
		sed -i "s/^allow_unsupported_modules.*/allow_unsupported_modules 1/" /etc/modprobe.d/10-unsupported-modules.conf; \
	fi)
	-modprobe mysm3
	-modprobe mytest


uninstall:
	modprobe -r mytest
	modprobe -r mysm3
	rm -rf /lib/modules/$(KERNEL_VERSION_BY_BUILDENV)/extra/mytest.ko
	rm -rf /lib/modules/$(KERNEL_VERSION_BY_BUILDENV)/extra/mysm3.ko
	depmod -a
	
clean:
	rm -rf $(TARGET) 
